package com.ldc.framework.fragment

import android.os.Bundle
import android.os.Message
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.ldc.commonlibs.listener.OnRowListener
import com.ldc.commonlibs.uts.ApiUtils
import com.ldc.commonlibs.uts.MessageUts
import com.ldc.commonlibs.uts.ViewModelUtils
import com.ldc.commonres.common.VBMFragment
import com.ldc.framework.activity.ArticleListActivity
import com.ldc.framework.adapter.SystemFragmentAdapter
import com.ldc.framework.databinding.FrameworkFragmentSystemBinding
import com.ldc.framework.model.SystemModel
import com.ldc.framework.viewmodel.FrameworkViewModelBase


class SystemFragment : VBMFragment<FrameworkFragmentSystemBinding, FrameworkViewModelBase>() {
    private var systemFragmentAdapter: SystemFragmentAdapter? = null


    companion object {
        private const val REFRESH_CODE = 0x123
        fun newInstance(): SystemFragment = SystemFragment().also {
            it.arguments = Bundle()
        }
    }

    override fun processMessage(msg: Message): Boolean {
        when (msg.what) {
            REFRESH_CODE -> {
                getSystemList()
            }
        }
        return super.processMessage(msg)
    }


    override fun main() {
        initAdapter()
        viewBind?.contentView?.let {
            it.refreshView.setEnableLoadMore(false)
            it.refreshView.setOnRefreshListener {
                MessageUts.sendMessage(getHandler(), REFRESH_CODE)
            }
        }

        viewBind?.statusView?.let {
            it.showLoadView()
            it.addRetryClickListener(object : OnRowListener<Int> {
                override fun onRow(v: View, data: Int) {
                    it.showLoadView("正在重试，请稍等")
                    MessageUts.sendMessage(getHandler(), REFRESH_CODE)
                }
            })
        }
        MessageUts.sendMessage(getHandler(), REFRESH_CODE)


    }

    override fun createViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FrameworkFragmentSystemBinding =
        FrameworkFragmentSystemBinding.inflate(inflater, container, false)

    override fun createViewModel(): FrameworkViewModelBase =
        ViewModelUtils.create(this, FrameworkViewModelBase::class.java)


    private fun initAdapter() {
        systemFragmentAdapter = SystemFragmentAdapter().also {
            it.addOnRowListener(object : OnRowListener<SystemModel.SystemModelItem.Children> {
                override fun onRow(v: View, data: SystemModel.SystemModelItem.Children) {
                    ArticleListActivity.start(requireContext(), data.name, "${data.id}")
                }
            })
        }
        viewBind?.contentView?.rv?.recyclerview?.also {
            it.adapter = systemFragmentAdapter
            it.layoutManager =
                LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
            it.setHasFixedSize(true)
        }

    }

    private fun getSystemList() {
        viewModel?.systemList()?.observe(this) {
            viewBind?.statusView?.showContentView()
            viewBind?.contentView?.refreshView?.let { rf ->
                if (rf.state.isOpening) {
                    rf.finishRefresh()
                }
            }
            if (ApiUtils.isSuccess(it)) {
                if (it.data?.size!! > 0) {
                    systemFragmentAdapter?.newData(it.data)
                } else {
                    viewBind?.statusView?.showEmptyView()
                }
            } else {
                viewBind?.statusView?.showErrorView(it.errorMsg)
            }
        }
    }

}